﻿using Models.ApiModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace webui.Controllers
{
    public class ApiCommonController : ApiController
    {
        [HttpPost]
        public async System.Threading.Tasks.Task<BaseRes>  UploadFile()
        {
          
            BaseRes res = new BaseRes();
            string savedir = @"C:\web\";
            try
            {

                var provider = new MultipartFormDataStreamProvider(savedir);
               await Request.Content.ReadAsMultipartAsync(provider);
                var formData = provider.FormData;
                // 获取文件数据
                if (provider.FileData.Count == 0)
                {
                    res.Code = -1;
                    res.Msg = "请上传文件";
                    return res;
                }

                foreach (MultipartFileData file in provider.FileData)
                {
                    //将本地文件转换为实际所需文件
                    string fname =Path.GetFileName( file.Headers.ContentDisposition.FileName.Replace("\"",""));
                    string savepdf = Path.Combine(savedir, fname);
                    if (!File.Exists(savepdf))
                    {
                        File.Move(file.LocalFileName, Path.Combine(savedir, savepdf));
                    }
                }
            }
            catch (Exception ce)
            {

                Logger.Default.Error(ce.Message, ce);
                res.Code = -2;
                res.Msg = ce.Message;

            }
            return res;
        }

    }
}
